<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String xm_id = request.getParameter("xm_id") == null ? "" : request.getParameter("xm_id");
	String xm_code = request.getParameter("xm_code") == null ? "" : request.getParameter("xm_code");
	String xm_name = request.getParameter("xm_name") == null ? "" : request.getParameter("xm_name");
	String customer_id = request.getParameter("customer_id") == null ? "" : request.getParameter("customer_id");
	String customer = request.getParameter("customer") == null ? "" : request.getParameter("customer");
	String id = request.getParameter("id") == null ? "" : request.getParameter("id");
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	boolean isAdd = "".equals(id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title>查看项目施工进度支付</title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		var xm_id="<%=xm_id%>"; 
		var supply_id;
		var isInit=0;
		$(function(){
			
			if('<%=flag%>' == 1){
				 $('#ff').form('load','<%=contextPath %>/springMVC/xmFinance/viewXmPay?id=<%=id%>');
			}
	       $('#table').datagrid({
	    	    <%=isAdd?"url:'"+contextPath+"/springMVC/xmFinance/listXMGDWorkerPayment',":"url:'"+contextPath+"/springMVC/xmFinance/viewXmPayWorkerDetail?id="+id+"',"%>
				rownumbers:true,
				fitColumns:true,
				nowrap:false,
				frozenColumns:[[
				    {title:'工单支付id',field:'id',hidden:true}
				]],
				columns:[[
					{title:'工单id',field:'gd_id',hidden:true},
					{title:'工单编号',field:'gd_code',width:250,align:'center'},
					{title:'工单名称',field:'gd_name',width:150,align:'center'},
					{title:'应付款',field:'finance_out',width:200,align:'center',formatter:function(value,rowData,index){
						if(isNaN(value)){
							return "未结算";
						}
						return value;
					}},
					{title:'现付款',field:'now_pay',width:100,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2,
							myFlag:'1'
						}
					}},
					{title:'备注',field:'remark',width:200,align:'center',editor:{
						type:'textarea'
					}}
				]],
				onBeforeLoad:function(param){
					$(this).datagrid('rejectChanges');
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'now_pay'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				toolbar:[
					{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						addPayValue();	
	
					}
					}
			   ]
			});
			
		});
		function addPayValue(){
		var jsondata=$("#table").datagrid("getData");
			var supplyPay=0;
			for(var i=0;i<jsondata.total;i++){
				var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
				if(jsondata.rows[i].now_pay == null || jsondata.rows[i].now_pay == "" || jsondata.rows[i].now_pay <= 0){
						$.messager.alert('提示','第'+index+'行支付金额有误！');
						return ;
				}else if(parseFloat(jsondata.rows[i].now_pay)>parseFloat(jsondata.rows[i].finance_out)){
						$.messager.alert('提示','第'+index+'行支付金额不能大于应付金额！');
						return ;
				}
				else{
					supplyPay=parseFloat(supplyPay)+parseFloat(jsondata.rows[i].now_pay);		
				}
			}
			jQuery("#pay_value").numberbox('setValue', supplyPay);						
		}
		function addRow(){
			$('#table').datagrid('endEdit', lastIndex);
			$('#table').datagrid('appendRow',{
			});
			var data = $('#table').datagrid('getData');
			$('#table').datagrid('loadData',data);
			lastIndex = $('#table').datagrid('getRows').length-1;
			$('#table').datagrid('selectRow', lastIndex);
			$('#table').datagrid('beginEdit', lastIndex);
		}
		function rtCallBackXmSet(pXm_id,xm_code,xm_name,deptId,dept,customer_id,customer){
		    xm_id=pXm_id;
			jQuery("#xm_id").val(pXm_id);
		    jQuery("#xm_name").val(xm_name);
		    jQuery("#xm_code").val(xm_code);
		    jQuery("#dept_name").val(dept);
		    jQuery("#dept_id").val(deptId);
		    jQuery("#customer_id").val(customer_id);
		    jQuery("#customer").val(customer);
		}
	</script>
	</head>
	<body>
		<div style="width: 97%;padding-left:10px">
		<form id="ff">
		<input  type="hidden" id="pay_to_id" name="pay_to_id" >
		<input  type="hidden" id="pay_to_name" name="pay_to_name" >
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>项目编号：</td>
					<td class="td2" nowrap>
						<input  id="xm_code" name="xm_code" readOnly value="<%=xm_code%>">
						<input type="hidden" id="xm_id" name="xm_id" readOnly value="<%=xm_id%>">
	
					</td>
					<td align="right" class="td1" nowrap>项目名称：</td>
					<td class="td2" nowrap>
						<input  id="xm_name" name="xm_name" readOnly value="<%=xm_name%>">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>项目部门：</td>
					<td class="td2" nowrap>
						<input  id="dept_name" name="dept_name" readOnly value="">
						<input  type="hidden" id="dept_id" name="dept_id" readOnly value="">
					</td>
					<td align="right" class="td1" nowrap>经办人：</td>
					<td class="td2" nowrap>
						<input  id="person" name="person" readOnly value="<%=person%>">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>对应客户：</td>
					<td class="td2" nowrap colspan="3">
						<input  id="customer" name="customer" readOnly value="">
						<input  type="hidden" id="customer_id" name="customer_id" readOnly value="">
					</td>
				</tr>
				<tr>
			      <td align="right" class="td1" nowrap>选择施工队：</td>
					<td class="td2" nowrap colspan="3">
						<input  id="pay_to_name" name="pay_to_name" readOnly>
					</td>	
				</tr>
			
				<tr>	
			        <td align="right" class="td1" nowrap>付款金额：</td>
					<td class="td2" nowrap colspan="3">
						<input  id="pay_value" name="pay_value" class="easyui-numberbox" data-options="min:0,precision:2" value="0" readonly>
					</td>
	
			       
				</tr>				
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='3'>
						<textarea  id="remark" name="remark" rows="6" style="width: 100%"></textarea>
					</td>
				</tr>
				<tr>
					<td colspan="4"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
			</form>
		</div>
		
	</body>
</html>